﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Youz.Common.Helper
{
    /// <summary>
    /// 验证码类
    /// </summary>
    public class ValidateCode
    {
        /// <summary>
        /// 产生6位随机数
        /// </summary>
        /// <param name="codeCount"></param>
        /// <returns></returns>
        public static string GetRandomNum(int codeCount=6)
        {
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(10);

                while (temp == t)
                {
                    t = rand.Next(10);
                }

                temp = t;
                RandomCode += t;
            }
            return RandomCode;
        }

        /// <summary>
        /// 产生四位随机码
        /// </summary>
        /// <param name="CodeCount">位数</param>
        /// <returns></returns>
        public static string GetRandomCode(int codeCount= 4)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;

            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(33);

                while (temp == t)
                {
                    t = rand.Next(33);
                }

                temp = t;
                RandomCode += allCharArray[t];
            }

            return RandomCode;
        }

        private static int letterWidth = 30;//单个字体的宽度范围
        private static string[] fonts = { "Arial", "Georgia" };
        public static MemoryStream CreateCodeImage(string checkCode)
        {
            int int_ImageWidth = checkCode.Length * letterWidth + 5;
            Random newRandom = new Random();
            Bitmap image = new Bitmap(125, 35);
            Graphics g = Graphics.FromImage(image);
            //生成随机生成器  
            Random random = new Random();
            //白色背景  
            g.Clear(Color.White);
            //画图片的背景噪音线  
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            //画图片的前景噪音点  
            for (int i = 0; i < 10; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            //随机字体和颜色的验证码字符  
            int findex;
            for (int int_index = 0; int_index < checkCode.Length; int_index++)
            {
                findex = newRandom.Next(fonts.Length - 1);
                string str_char = checkCode.Substring(int_index, 1);
                Brush newBrush = new SolidBrush(GetRandomColor());
                Point thePos = new Point(int_index * letterWidth + newRandom.Next(3), 1 + newRandom.Next(3));
                g.DrawString(str_char, new Font(fonts[findex], 20, FontStyle.Bold), newBrush, thePos);
            }
            //灰色边框  
            //g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, 119, 34);
            //图片扭曲  
            //image = TwistImage(image, true, 3, 4);  
            //将生成的图片发回客户端  
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            g.Dispose();
            image.Dispose();
            return ms;
        }

        public static Color GetRandomColor()
        {
            Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
            System.Threading.Thread.Sleep(RandomNum_First.Next(50));
            Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
            int int_Red = RandomNum_First.Next(210);
            int int_Green = RandomNum_Sencond.Next(180);
            int int_Blue = (int_Red + int_Green > 300) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            return Color.FromArgb(int_Red, int_Green, int_Blue);
        }
    }
}
